package com.ruoyi.tcod.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.system.domain.vo.SysOssVo;
import com.ruoyi.system.service.ISysOssService;
import com.ruoyi.tcod.domain.bo.TcImgOrderApproveBo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.tcod.domain.bo.TcImgOrderBo;
import com.ruoyi.tcod.domain.vo.TcImgOrderVo;
import com.ruoyi.tcod.domain.TcImgOrder;
import com.ruoyi.tcod.mapper.TcImgOrderMapper;
import com.ruoyi.tcod.service.ITcImgOrderService;

import java.util.List;
import java.util.Map;
import java.util.Collection;
import java.util.stream.Collectors;

/**
 * 单子Service业务层处理
 *
 * @author ruoyi
 * @date 2024-10-20
 */
@RequiredArgsConstructor
@Service
public class TcImgOrderServiceImpl implements ITcImgOrderService {

    private final TcImgOrderMapper baseMapper;
    private final ISysOssService ossService;

    /**
     * 查询单子
     */
    @Override
    public TcImgOrderVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询单子列表
     */
    @Override
    public TableDataInfo<TcImgOrderVo> queryPageList(TcImgOrderBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TcImgOrder> lqw = buildQueryWrapper(bo);
        Page<TcImgOrderVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        if(CollUtil.isNotEmpty(result.getRecords())){
            setImgUrl(result.getRecords());
        }
        return TableDataInfo.build(result);
    }

    /**
     * 查询单子列表
     */
    @Override
    public List<TcImgOrderVo> queryList(TcImgOrderBo bo) {
        LambdaQueryWrapper<TcImgOrder> lqw = buildQueryWrapper(bo);
        List<TcImgOrderVo> list = baseMapper.selectVoList(lqw);
        setImgUrl(list);
        return list;
    }

    private void setImgUrl(List<TcImgOrderVo> list) {
        if(CollUtil.isNotEmpty(list)){
            List<Long> ossIds = list.stream()
                .map(TcImgOrderVo::getOssId)
                .collect(Collectors.toList());
            List<SysOssVo> ossVos = ossService.listByIds(ossIds);
            if(CollUtil.isNotEmpty(ossVos)){
                Map<Long, String> ossMap = ossVos.stream().collect(Collectors.toMap(SysOssVo::getOssId, SysOssVo::getUrl));
                list.stream().peek(vo -> {
                    vo.setStateDesc(1 == vo.getState() ? "待审核" : (2 == vo.getState() ? "审核成功" : "审核失败"));
                    vo.setImgUrl(ossMap.get(vo.getOssId()));
                }).collect(Collectors.toList());
            }
        }
    }

    private LambdaQueryWrapper<TcImgOrder> buildQueryWrapper(TcImgOrderBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TcImgOrder> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getUserId() != null, TcImgOrder::getUserId, bo.getUserId());
        lqw.eq(StringUtils.isNotBlank(bo.getTitle()), TcImgOrder::getTitle, bo.getTitle());
        lqw.eq(bo.getCategoryId() != null, TcImgOrder::getCategoryId, bo.getCategoryId());
        lqw.eq(bo.getState() != null, TcImgOrder::getState, bo.getState());
        lqw.between(params.get("beginCreateTime") != null && params.get("endCreateTime") != null,
            TcImgOrder::getCreateTime ,params.get("beginCreateTime"), params.get("endCreateTime"));
        return lqw;
    }

    /**
     * 新增单子
     */
    @Override
    public Boolean insertByBo(TcImgOrderBo bo) {
        TcImgOrder add = BeanUtil.toBean(bo, TcImgOrder.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改单子
     */
    @Override
    public Boolean updateByBo(TcImgOrderBo bo) {
        TcImgOrder update = BeanUtil.toBean(bo, TcImgOrder.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TcImgOrder entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除单子
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public void approve(TcImgOrderApproveBo bo) {
        LambdaUpdateWrapper<TcImgOrder> wrapper = Wrappers.lambdaUpdate();
        wrapper.set(TcImgOrder::getState,1 == bo.getResult() ? 2 : 3)
            .eq(TcImgOrder::getId,bo.getId())
            .eq(TcImgOrder::getState,1);
        baseMapper.update(wrapper);
    }
}
